﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace IPR_TrainExamSys_TY.SysSet
{
    public partial class FrmPowerMan : Form
    {
        public FrmPowerMan()
        {
            InitializeComponent();
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ckbx001_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbx001.Checked)
            {
                ckbx006.Checked = true;
                ckbx007.Checked = true;
                ckbx008.Checked = true;
            }
            else
            {
                ckbx006.Checked = false;
                ckbx007.Checked = false;
                ckbx008.Checked = false;
            }
        }

        private void ckbx002_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbx002.Checked)
            {
                ckbx009.Checked = true;
                ckbx010.Checked = true;
            }
            else
            {
                ckbx009.Checked = false;
                ckbx010.Checked = false;
            }
        }

        private void ckbx003_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbx003.Checked)
            {
                ckbx011.Checked = true;
                ckbx015.Checked = true;
            }
            else
            {
                ckbx011.Checked = false;
                ckbx015.Checked = false;
            }
        }

        private void ckbx004_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbx004.Checked)
            {
                ckbx012.Checked = true;
                ckbx013.Checked = true;
                ckbx016.Checked = true;
            }
            else
            {
                ckbx012.Checked = false;
                ckbx013.Checked = false;
                ckbx016.Checked = false;
            }
        }

        private void ckbx005_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbx005.Checked)
            {
                ckbx014.Checked = true;
            }
            else
            {
                ckbx014.Checked = false;
            }
        }

        private void FrmPowerMan_Load(object sender, EventArgs e)
        {
            initTreeNode();
        }

        private void initTreeNode()
        {
            advTree1.Nodes.Clear();
            #region 初始化菜单和功能栏
            string sWhere = " IsDelete=0 ";
            DataSet dsFun = new T_PublicDepartment().GetList(sWhere);
            if (dsFun != null)
            {
                if (dsFun.Tables.Count > 0)
                    if (dsFun.Tables[0].Rows.Count > 0)
                    {
                        //菜单自定义增加
                        DataRow[] dr = dsFun.Tables[0].Select(" DepartMentPID=0 and DepartMentLevel=0 ", " DepartmentId ASC");
                        for (int i = 0; i < dr.Count(); i++)
                        {
                            DevComponents.AdvTree.Node tnNew = new DevComponents.AdvTree.Node(dr[i]["DepartmentName"].ToString());
                            tnNew.Tag = dr[i]["DepartmentId"].ToString();
                            tnNew.Image = imageList1.Images[int.Parse(dr[i]["DepartMentLevel"].ToString())];
                            tnNew.NodeClick += tnNew_NodeClick;
                            AddTreeNode(tnNew, dsFun, dr[i]["DepartmentId"].ToString());
                            advTree1.Nodes.Add(tnNew, DevComponents.AdvTree.eTreeAction.Expand);
                            Application.DoEvents();
                        }
                        advTree1.ExpandAll();

                    }
            }
            #endregion
        }

        /// <summary>
        /// 添加对应的菜单项
        /// </summary>
        /// <param name="bi"></param>
        /// <param name="dsFun"></param>
        /// <param name="DepartmentId"></param>
        private void AddTreeNode(DevComponents.AdvTree.Node myNode, DataSet dsFun, string DepartmentId)
        {
            DataRow[] dr = dsFun.Tables[0].Select(" DepartMentPID='" + DepartmentId + "' ", " DepartmentId ASC");
            if (dr.Count() > 0)
            {
                for (int i = 0; i < dr.Count(); i++)
                {
                    DevComponents.AdvTree.Node tnNew = new DevComponents.AdvTree.Node(dr[i]["DepartmentName"].ToString());
                    tnNew.Tag = dr[i]["DepartmentId"].ToString();
                    tnNew.Image = imageList1.Images[int.Parse(dr[i]["DepartMentLevel"].ToString())];
                    tnNew.NodeClick += tnNew_NodeClick;
                    AddTreeNode(tnNew, dsFun, dr[i]["DepartmentId"].ToString());
                    myNode.Nodes.Add(tnNew, DevComponents.AdvTree.eTreeAction.Expand);
                    Application.DoEvents();
                }
            }
        }


        void tnNew_NodeClick(object sender, EventArgs e)
        {
            DevComponents.AdvTree.Node myNode = (DevComponents.AdvTree.Node)sender;
            if (myNode.Level == 0)
            {
                //tbJWD.Text = myNode.Text;
                //lbJWDID.Text = myNode.Tag.ToString();
                lbCurrent.Text = myNode.Text;
            }
            if (myNode.Level == 1)
            {
                //tbJWD.Text = myNode.Parent.Text;
                //tbKS.Text = myNode.Text;
                //lbJWDID.Text = myNode.Parent.Tag.ToString();
                //lbKSID.Text = myNode.Tag.ToString();
                lbCurrent.Text = myNode.Parent.Text + "--" + myNode.Text;
            }
            if (myNode.Level == 2)
            {
                //tbJWD.Text = myNode.Parent.Parent.Text;
                //tbKS.Text = myNode.Parent.Text;
                //tbBZ.Text = myNode.Text;
                //lbJWDID.Text = myNode.Parent.Parent.Tag.ToString();
                //lbKSID.Text = myNode.Parent.Tag.ToString();
                //lbBZID.Text = myNode.Tag.ToString();
                lbCurrent.Text = myNode.Parent.Parent.Text + "--" + myNode.Parent.Text + "--" + myNode.Text;
            }

            lbCurDepartID.Text = myNode.Tag.ToString();
            tbCurDepartName.Text = myNode.Text;
            lbCurLevel.Text = myNode.Level.ToString();

            if (myNode.Parent != null)
                lbParentID.Text = myNode.Parent.Tag.ToString();
            else lbParentID.Text = "0";

            getPowerbyDepName(tbCurDepartName.Text.Trim());
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            string str = "";//权限
            foreach (object o in pnPower.Controls)
            {
                DevComponents.DotNetBar.Controls.CheckBoxX cbx = o as DevComponents.DotNetBar.Controls.CheckBoxX;
                if (cbx.Checked)
                {
                    str += cbx.Tag + ",";
                }
            }
            if (str != "")
            {
                str = str.Substring(0, str.Length - 1);
            }
            string strdep=tbCurDepartName.Text;//部门名字
            if (strdep.Trim() == "")
            {
                MessageBox.Show("请先选择部门");
                return;
            }
            else if(str=="")
            {
                MessageBox.Show("您没有选择任何模块");
                return;
            }
            int count = new BLL.T_DepPower().setPower(strdep, str);
            if (count > 0)
            {
                MessageBox.Show("设置成功");
            }
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            foreach (object o in pnPower.Controls)
            {
                DevComponents.DotNetBar.Controls.CheckBoxX cbx = o as DevComponents.DotNetBar.Controls.CheckBoxX;
                if (cbx.Checked)
                {
                    cbx.Checked = false;
                }
            }
        }

        private void getPowerbyDepName(string depName)
        {
            btnclear_Click(null,null);

            string power = new BLL.T_DepPower().getPowerByDepName(depName);
            if(power.Length>0)
            {
            string[] powers = power.Split(',');
                foreach (string str in powers)
                {
                    string ckbx = "ckbx" + str;
                    (pnPower.Controls.Find(ckbx, false)[0] as DevComponents.DotNetBar.Controls.CheckBoxX).Checked = true;
                }
            }
        }
    }
}
